package com.lingnan.wanxiangxinxun.controller.admin;


import com.lingnan.wanxiangxinxun.entity.po.TempFile;
import com.lingnan.wanxiangxinxun.entity.vo.common.Result;
import com.lingnan.wanxiangxinxun.service.ITempFileService;
import com.lingnan.wanxiangxinxun.util.AliOSSUtils;
import com.lingnan.wanxiangxinxun.util.jwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 通用接口
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {


    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private ITempFileService tempFileService;


    /**
     * 文件上传
     * @param file
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file, String fullPath)  {
        try{
            String filePath = aliOSSUtils.upload(file, fullPath); //文件的请求路径
            return Result.success(filePath);
        } catch (Exception e) {
            log.error("文件上传失败：", e);
        }

        return Result.error("文件上传失败");
    }


    /**
     * 删除文件
     */
    @PostMapping("/deleteObject")
    public Result delete(@RequestBody List<String> fullPathList) {
        log.info("删除oss文件，请求参数为：{}", fullPathList);

        try {
            for(String fullPath: fullPathList){
                aliOSSUtils.deleteObject(fullPath);
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error(String.valueOf(e));
        }
    }


    //将文件路径存入临时文件标记表
    @PostMapping("/addTempFile")
    public Result addTempFile(@RequestParam String fullPath) {
        log.info("将文件路径存入临时文件标记表，请求参数为：{}", fullPath);
        if(fullPath == null || fullPath.isEmpty()){
            return Result.error("文件路径不允许为空");
        }

        TempFile tf = new TempFile();
        tf.setFileUrl(fullPath);
        tf.setCreateTime(LocalDateTime.now());
        tempFileService.save(tf);
        return Result.success();
    }


    //临时文件标记表删除信息
    @PostMapping("/deleteTempFile")
    public Result deleteTempFile(@RequestBody List<String> fullPathList) {
        if(fullPathList == null || fullPathList.isEmpty()){
            return Result.error("删除目标为空，不执行删除");
        }
        else {
            boolean res = tempFileService.removeByFullPath(fullPathList);
            if(!res){
                return Result.error("删除失败");
            }
            return Result.success();
        }
    }



    /**
     * 解析Jwt令牌
     * @param Jwt
     * @return
     */
    @GetMapping("/ParseJwt/{Jwt}")
    public Result ParseJwt(@PathVariable String Jwt){

        Object obj;
        try {
            obj = jwtUtils.ParseJwt(Jwt); //解析令牌

        } catch (Exception e) {
            log.info("令牌解析错误！");
            return Result.error("令牌解析错误！");
        }

        //log.info("令牌解析成功！");
        return Result.success(obj);
    }


}
